const base = require("./base");
const Code = require("./../../../../lib/wxa/code.class");

module.exports = class extends base {
    async _initialize() {
        await super.isAppAuth();
    }

    async index() {
        const { page = 1 } = this.query;
        const appId = this.state.app.id;
        const wxa = await this.model("wxa")
            .query(qb => {
                qb.where("app_id", appId);
            })
            .fetch();
        if (wxa) {
            const wxaplugins = await this.model("wxaplugins")
                .query(qb => {
                    qb.where("wxa_id", wxa.id);
                    qb.orderBy("id", "desc");
                })
                .fetchPage({
                    page: page
                });
            this.success(wxaplugins);
        } else {
            this.fail("小程序未授权");
        }
    }

    async add() {
        const appId = this.state.app.id;
        const { name, appid } = this.post;

        let wxa = await this.model("wxa")
            .query(qb => {
                qb.where("app_id", appId);
            })
            .fetch();

        if (wxa) {
            const code = new Code(
                process.env.OPEN_APPID,
                process.env.OPEN_APPSECRET
            );
            try {
                wxa = await this.checkWxaAuthorizerAccessToken(wxa);
            } catch (err) {
                this.status = 500;
                throw err;
            }
            const result = await code.wxa_plugin(
                wxa.authorizer_access_token,
                "apply",
                appid
            );
            console.log("添加插件结果", result);
            if (result.errmsg === "ok") {
                console.log("添加插件成功");

                await this.model("wxaplugins")
                    .forge({ wxa_id: wxa.id, name: name, appid: appid })
                    .save();
                this.success("添加插件成功");
            } else {
                console.log("添加插件失败");
                this.fail(result.errmsg);
            }
        } else {
            this.fail("小程序未授权");
        }
    }

    async del() {
        const { id } = this.query;
        const appId = this.state.app.id;

        const wxaplugin = await this.model("wxaplugins")
            .query(qb => {
                qb.where("id", id);
            })
            .fetch();

        let wxa = await this.model("wxa")
            .query(qb => {
                qb.where("app_id", appId);
            })
            .fetch();

        const code = new Code(
            process.env.OPEN_APPID,
            process.env.OPEN_APPSECRET
        );
        try {
            wxa = await this.checkWxaAuthorizerAccessToken(wxa);
        } catch (err) {
            this.status = 500;
            throw err;
        }
        const result = await code.wxa_plugin(
            wxa.authorizer_access_token,
            "unbind",
            wxaplugin.appid
        );
        console.log("删除插件返回值", result);
        if (result.errcode === 0 || result.errcode === 89244) {
            await this.model("wxaplugins")
                .query(qb => {
                    qb.where("id", id);
                })
                .destroy();
            this.success("删除成功");
        } else {
            this.fail(result.errmsg);
        }
    }
};
